package ace.module.oauth2.server.core.impl.service.impl;

import ace.module.oauth2.server.core.impl.service.Oauth2TokenService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.oauth2.server.authorization.OAuth2Authorization;
import org.springframework.security.oauth2.server.authorization.OAuth2AuthorizationService;
import org.springframework.stereotype.Component;

/**
 * @author caspar
 * @date 2023/10/14 10:38
 */
@Component
@AllArgsConstructor
@Slf4j
public class Oauth2TokenServiceImpl implements Oauth2TokenService {
  private final OAuth2AuthorizationService authorizationService;

  public Boolean removeToken(String token) {
    if (StringUtils.isEmpty(token)) {
      return Boolean.TRUE;
    }
    OAuth2Authorization oAuth2Authorization = authorizationService.findByToken(token, null);
    if (oAuth2Authorization == null) {
      return Boolean.TRUE;
    }
    this.authorizationService.remove(oAuth2Authorization);
    return Boolean.TRUE;
  }
}
